package com.example.caorl.jdbcaop.dao.impl;


import com.example.caorl.jdbcaop.dao.IStudentDAO;
import com.example.caorl.jdbcaop.domain.Student;
import com.example.caorl.jdbcaop.handler.impl.BeanHandler;
import com.example.caorl.jdbcaop.handler.impl.StudentHandler;
import com.example.caorl.jdbcaop.util.JDBCTemplate;

import java.util.ArrayList;
import java.util.List;

public class StudentTemplateDAOImpl implements IStudentDAO {

    private JDBCTemplate jdbcTemplate;

//    private ConnectionUtils connectionUtils;

//    public void setConnectionUtils(ConnectionUtils connectionUtils) {
//        this.connectionUtils = connectionUtils;
//    }

    public void setJdbcTemplate(JDBCTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void save(Student stu) {
        String sql="INSERT INTO student VALUES(?,?,?)";
        Object[] args=new Object[]{stu.getId(),stu.getName(),stu.getAge()};
        jdbcTemplate.update(sql, args);
    }

    public void delete(Long id) {
        String sql="DELETE FROM student WHERE id=?";
        jdbcTemplate.update(sql, id);
    }

    public void update(Student stu) {
        String sql="UPDATE student SET name=?,age=? WHERE id=?";
        Object[] args=new Object[]{stu.getName(),stu.getAge(),stu.getId()};
        jdbcTemplate.update(sql, args);
    }

    public Student get(Long id) {
        //List<Student> list=new ArrayList<>();
        String sql="SELECT * FROM student WHERE id=?";

        //list=JDBCTemplate.queryArt(sql,new StudentHandler(),id);//这里的返回值是List<Student>
        Student stu=jdbcTemplate.queryBean(sql,new BeanHandler<Student>(Student.class),id);
        return stu;
    }

    public List<Student> list() {
        List<Student> list=new ArrayList<>();
        String sql="SELECT * FROM student";
        //这里不要写参数
        list=jdbcTemplate.queryArt(sql,new StudentHandler());
        return list;
    }
}
